{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a9f51513",
   "metadata": {},
   "source": [
    "# 1.字典的初始（key:value)\n",
    "\n",
    "## 1.1 字典的重要属性\n",
    "* 1.结构：key:value(键值对)\n",
    "* 2.字典是无序的，我们通过key去查找value，就好像list用index查找value一样，只不过字典的“索引”被定义了名字\n",
    "* 3.字典的key的定义有一定的要求：key是由“不可变”的数据结构才可以定义，list是不能作为key来定义的\n",
    "* 4.字典的key是唯一的，字典的value也可以不唯一(list的值不是唯一的）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1807b899",
   "metadata": {},
   "outputs": [],
   "source": [
    "users = [\n",
    "    [\"xiaoma\",11,\"stu\",20000],\n",
    "    [\"xiaoming\",22,\"stu\",30000]\n",
    "\n",
    "]\n",
    "# 试对比\n",
    "users_dict = {\n",
    "    \"xiaoma\":[11,\"stu\",30000],\n",
    "    \"xiaoming\":[22,\"stu\",30000]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "162a4e80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['xiaoming', 22, 'stu', 30000]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询xiaoming 的信息\n",
    "users[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0d3c77f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[22, 'stu', 30000]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users_dict[\"xiaoming\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "259f2b7a",
   "metadata": {},
   "source": [
    "* 试问：如果users的数据很大，想查其中一个信息，list就变得很无力,而dict只需要知道数据的关键信息即可"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "625bc25b",
   "metadata": {},
   "source": [
    "## 1.2字典的创建（常见的三种创建方式）\n",
    "* 1.info = {key1:values1, key2:value2, ...}直接按照字典格式创建\n",
    "* 2.info_02 = dict(key1=value1, key2=value2, ...)使用dict()进行字典创建\n",
    "* 3.运用字典添加的方式新建字典：info_03 = {} info_03[key1]=value1,info_03[key2]=value2 ..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7b5d442",
   "metadata": {},
   "source": [
    "### 直接按照字典格式创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8b7be067",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 第一种\n",
    "info = {\n",
    "    \"stu10001\":\"xiaoming\",\n",
    "    \"stu10002\":\"lihua\",\n",
    "    \"stu10003\":\"xiaohong\"\n",
    "\n",
    "}\n",
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "346f876f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiao', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# value是可以重复出现的，key唯一\n",
    "# 如果有重复的key，新的key的值会代替原来的\n",
    "info = {\n",
    "    \"stu10001\":\"xiaoming\",\n",
    "    \"stu10002\":\"lihua\",\n",
    "    \"stu10003\":\"xiaohong\",\n",
    "    \"stu10001\":\"xiao\"\n",
    "}\n",
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac220b7d",
   "metadata": {},
   "source": [
    "### 使用dict()进行字典创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "511da8af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 第二种\n",
    "info_02 = dict(\n",
    "    stu10001=\"xiaoming\",\n",
    "    stu10002=\"lihua\",\n",
    "    stu10003=\"xiaohong\",\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "959ee30f",
   "metadata": {},
   "source": [
    "### 运用字典添加的方式新建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "76116e32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 第三种\n",
    "info_03 = {} # 新建空字典\n",
    "info_03[\"stu10001\"]=\"xiaoming\"\n",
    "info_03[\"stu10002\"]=\"lihua\"\n",
    "info_03[\"stu10003\"]=\"xiaohong\"\n",
    "print(info_03)\n",
    "print(type(info_03))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9aa309a",
   "metadata": {},
   "source": [
    "## 1.3字典的增、删、改、查\n",
    "### 1.字典的增加(和字典的第三种创建方式一致)\n",
    "* info_03[\"stu10004\"] = \"xiaoma\"\n",
    "### 2.字典的查询\n",
    "* info03[key_name]  \n",
    "* 注意：正确的查询方式，应该先查询key是否存在字典当中，如下 \n",
    "* A.key_name in info_03  ## 查看是否存在，返回结果true\n",
    "* B.info_03[key_name]\n",
    "### 3.字典的修改\n",
    "* A.查询的key是否存在\n",
    "* B.修改key的值\n",
    "* 例如：修改info_03中stu10003的值为Alex，if\"stu10003\" in info_03:info_03[]\n",
    "### 4.字典的删除\n",
    "* info_03.pop(\"stu10003\")\n",
    "* info_03.popitem()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "272d8d8e",
   "metadata": {},
   "source": [
    "### 1.字典的增加(和字典的第三种创建方式一致)\n",
    "* info_03[\"stu10004\"] = \"xiaoma\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b9865170",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu_10001': 'xiaoming', 'stu_10002': 'lihua', 'stu_10003': 'xiaohong', 'stu10004': 'xiaoma'}\n"
     ]
    }
   ],
   "source": [
    "info_03[\"stu10004\"] = \"xiaoma\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc34a1be",
   "metadata": {},
   "source": [
    "### 2.字典的查询\n",
    "* info03[key_name]  \n",
    "* 注意：正确的查询方式，应该先查询key是否存在字典当中，如下 \n",
    "* A.key_name in info_03  ## 查看是否存在，返回结果true\n",
    "* B.info_03[key_name]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c06111b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "xiaohong\n"
     ]
    }
   ],
   "source": [
    "# 正确的查询字典方式\n",
    "print(\"stu10003\" in info_03)\n",
    "if \"stu10003\" in info_03:\n",
    "    print(info_03[\"stu10003\"])\n",
    "else:\n",
    "    print(\"查询的key不存在\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce0284c4",
   "metadata": {},
   "source": [
    "### 3.字典的修改\n",
    "* A.查询的key是否存在\n",
    "* B.修改key的值\n",
    "* 例如：修改info_03中stu10003的值为Alex，if\"stu10003\" in info_03:info_03[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "bd729790",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'Alex'}\n"
     ]
    }
   ],
   "source": [
    "if\"stu10003\" in info_03:\n",
    "    info_03[\"stu10003\"] = \"Alex\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "b6f62104",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on dict object:\n",
      "\n",
      "class dict(object)\n",
      " |  dict() -> new empty dictionary\n",
      " |  dict(mapping) -> new dictionary initialized from a mapping object's\n",
      " |      (key, value) pairs\n",
      " |  dict(iterable) -> new dictionary initialized as if via:\n",
      " |      d = {}\n",
      " |      for k, v in iterable:\n",
      " |          d[k] = v\n",
      " |  dict(**kwargs) -> new dictionary initialized with the name=value pairs\n",
      " |      in the keyword argument list.  For example:  dict(one=1, two=2)\n",
      " |  \n",
      " |  Built-in subclasses:\n",
      " |      StgDict\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      True if the dictionary has the specified key, else False.\n",
      " |  \n",
      " |  __delitem__(self, key, /)\n",
      " |      Delete self[key].\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(...)\n",
      " |      x.__getitem__(y) <==> x[y]\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __reversed__(self, /)\n",
      " |      Return a reverse iterator over the dict keys.\n",
      " |  \n",
      " |  __setitem__(self, key, value, /)\n",
      " |      Set self[key] to value.\n",
      " |  \n",
      " |  __sizeof__(...)\n",
      " |      D.__sizeof__() -> size of D in memory, in bytes\n",
      " |  \n",
      " |  clear(...)\n",
      " |      D.clear() -> None.  Remove all items from D.\n",
      " |  \n",
      " |  copy(...)\n",
      " |      D.copy() -> a shallow copy of D\n",
      " |  \n",
      " |  get(self, key, default=None, /)\n",
      " |      Return the value for key if key is in the dictionary, else default.\n",
      " |  \n",
      " |  items(...)\n",
      " |      D.items() -> a set-like object providing a view on D's items\n",
      " |  \n",
      " |  keys(...)\n",
      " |      D.keys() -> a set-like object providing a view on D's keys\n",
      " |  \n",
      " |  pop(...)\n",
      " |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      " |      If key is not found, d is returned if given, otherwise KeyError is raised\n",
      " |  \n",
      " |  popitem(self, /)\n",
      " |      Remove and return a (key, value) pair as a 2-tuple.\n",
      " |      \n",
      " |      Pairs are returned in LIFO (last-in, first-out) order.\n",
      " |      Raises KeyError if the dict is empty.\n",
      " |  \n",
      " |  setdefault(self, key, default=None, /)\n",
      " |      Insert key with a value of default if key is not in the dictionary.\n",
      " |      \n",
      " |      Return the value for key if key is in the dictionary, else default.\n",
      " |  \n",
      " |  update(...)\n",
      " |      D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.\n",
      " |      If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]\n",
      " |      If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v\n",
      " |      In either case, this is followed by: for k in F:  D[k] = F[k]\n",
      " |  \n",
      " |  values(...)\n",
      " |      D.values() -> an object providing a view on D's values\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  fromkeys(iterable, value=None, /) from builtins.type\n",
      " |      Create a new dictionary with keys from iterable and values set to value.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  __hash__ = None\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info_03)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99122fe3",
   "metadata": {},
   "source": [
    "### 4.字典的删除\n",
    "* info_03.pop(\"stu10003\")\n",
    "* info_03.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "7ea650eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua'}\n"
     ]
    }
   ],
   "source": [
    "info_03.pop(\"stu10003\")\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e9b020f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming'}\n"
     ]
    }
   ],
   "source": [
    "info_03.popitem()\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7435c3b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
